package com.webapp.org.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.webapp.org.bean.ResultDateBean;
import com.webapp.org.interfaces.IResultDate;
import com.webapp.org.util.condatabase.HBConfigure;

public class ResultDateDAO implements IResultDate
{
	private SessionFactory sf = HBConfigure.getSessionFactory();
	private boolean result = false;

	/* Method to insert an ResultDate from the records */
	public boolean insertResultDate(ResultDateBean resultDateBean)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			session.persist(resultDateBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception exception)
		{
			session.getTransaction().rollback();
			System.out.println("Error occured while inserting");
		} finally
		{
			session.close();
		}
		return result;
	}

	/* Method to update an ResultDateBean from the records */
	public boolean updateResultDate(ResultDateBean resultDateBean)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			session.update(resultDateBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("Exception took pplace in update");
		} finally
		{
			session.close();
		}
		return result;
	}

	/* Method to DELETE an resultDateBean from the records */
	public boolean deleteResultDate(int resultDateId)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{

			ResultDateBean resultDateBean = (ResultDateBean) session.get(
					ResultDateBean.class, resultDateId);
			session.delete(resultDateBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("error while deleting");
		} finally
		{
			session.close();
		}
		return result;
	}

	/* Method to retrieve an resultDateBean from the records */
	public List<ResultDateBean> retrieveResultDate()
	{
		List<ResultDateBean> ResultDateList = null;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			ResultDateList = session.createQuery("FROM ResultDateBean").list();
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("Error while retriving data");
		} finally
		{
			session.close();
		}
		return ResultDateList;
	}

	/* Method to retrieve an resultdate from the records */
	public ResultDateBean getResultDate(int resultDateId)
	{
		ResultDateBean resultDate = null;
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			resultDate = (ResultDateBean) session.get(ResultDateBean.class,
					resultDateId);
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("Error while retriving data");
		} finally
		{
			session.close();
		}
		return resultDate;
	}

}
